package cn.com.pajx.pajx_visitor_v3.ui.view.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import com.pajx.facelibrary.utils.DensityUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Objects;

import cn.com.pajx.pajx_visitor_v3.R;
import cn.com.pajx.pajx_visitor_v3.bean.EventMessage;
import cn.com.pajx.pajx_visitor_v3.utils.AppConstant;
import cn.com.pajx.pajx_visitor_v3.utils.DialogUtil;


/**
 * 自定义加载进度对话框
 * Created by ruancw on 2024/2/28.
 */

public class LoadingDialog extends AlertDialog {

    private Context context;
    private TextView tvMessage, tvDownloadProgress;
    private LinearLayout viewProgress;
    private String message = "努力加载中", download_progress = "0%";
    private boolean isDownLoad;

    public LoadingDialog(Context context) {
        super(context);
    }

    public LoadingDialog(Context context, boolean isDownLoad) {
        super(context);
        this.context = context;
        this.isDownLoad = isDownLoad;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loading_progress_layout);
        EventBus.getDefault().register(this);
        initConfig();
        //设置View初始化参数
        viewProgress = findViewById(R.id.view_progress);
        tvMessage = findViewById(R.id.tv_message);
        tvDownloadProgress = findViewById(R.id.tv_download_progress);
        tvMessage.setText(message);
        if (isDownLoad) {
            viewProgress.setPadding(DensityUtil.dp2px(context, 56), DensityUtil.dp2px(context, 20), DensityUtil.dp2px(context, 56), DensityUtil.dp2px(context, 20));
            tvDownloadProgress.setVisibility(View.VISIBLE);
        }
    }

    private void initConfig() {
        //设置背景透明，不然会出现白色直角问题
        Window window = getWindow();
        if (window != null) {
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }
        getWindow().setDimAmount(0.0f);
        setCanceledOnTouchOutside(false);
        setCancelable(false);
        DialogUtil.focusNotAle(getWindow());
        DialogUtil.hideNavigationBar(getWindow());//隐藏导航栏
        DialogUtil.clearFocusNotAle(getWindow());
    }

    /**
     * 初始化Dialog的参数
     */
    private void initParams() {
        //设置参数必须在show之后，不然没有效果
        WindowManager m = Objects.requireNonNull(getWindow()).getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高度
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = (int) (d.getWidth() * 0.35); // 宽度设置为屏幕的0.8，根据实际情况调整
        getWindow().setAttributes(params);
        //解决不显示输入法的问题
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    }

    /**
     * 为加载进度个对话框设置不同的提示消息
     *
     * @param message 给用户展示的提示信息
     * @return build模式设计，可以链式调用
     */
    public LoadingDialog setMessage(String message) {
        this.message = message;
        return this;
    }

    public void setDownloadProgress(String download_progress) {
        this.download_progress = download_progress;
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBusMessage(EventMessage bean) {
        if (TextUtils.equals(AppConstant.DOWNLOAD_PROGRESS,bean.getMessage())) {
            tvDownloadProgress.setText(bean.getData());
        }
    }
}
